using Engine.Graphics.VertexStructures;
using SharpDX;
using System.IO;

namespace Engine.FileFormats3D.C11Syndikat
{
    internal class C11VertexBlock
    {
        #region Private Fields

        private MyOwnVertex.PositionNormalTextured[] engineVertices;
        private C11Vertex[] vertices;
        private object[] wr2Data;

        #endregion Private Fields

        #region Internal Constructors

        internal C11VertexBlock()
        { }

        #endregion Internal Constructors

        #region Public Properties

        public MyOwnVertex.PositionNormalTextured[] EngineVertices
        {
            get { return engineVertices; }
            set { engineVertices = value; }
        }

        public object[] Wr2Data
        {
            get
            {
                return wr2Data;
            }

            set
            {
                wr2Data = value;
            }
        }

        #endregion Public Properties

        #region Internal Properties

        internal C11Vertex[] Vertices
        {
            get { return vertices; }
            set { vertices = value; }
        }

        #endregion Internal Properties

        #region Internal Methods

        internal static C11VertexBlock FromStream(BinaryReader br, int numberOfVertices)
        {
            C11VertexBlock block = new C11VertexBlock();
            block.Vertices = new C11Vertex[numberOfVertices];
            block.engineVertices = new MyOwnVertex.PositionNormalTextured[numberOfVertices];
            block.wr2Data = new object[numberOfVertices];

            for (int i = 0; i < block.Vertices.Length; i++)
            {
                block.Vertices[i] = C11Vertex.FromStream(br);
                C11Vertex c11Vertex = block.Vertices[i];

                block.engineVertices[i] = new MyOwnVertex.PositionNormalTextured()
                {
                    // Position
                    Position = new Vector3(c11Vertex.X, c11Vertex.Y, c11Vertex.Z),
                    // Nomalized normal
                    Normal = Vector3.Normalize(new Vector3(c11Vertex.NormalX, c11Vertex.NormalY, c11Vertex.NormalZ)),
                    // Texture Coordinates #1
                    UV = new Vector2(c11Vertex.TextureU, c11Vertex.TextureV),
                };
                block.wr2Data[i] = new MyOwnVertex.C11SyndicateVertexData()
                {
                    // Texture Coordinates #2
                    UV = new Vector2(c11Vertex.TextureW, c11Vertex.TextureX),
                    // Diffuse Color
                    DiffuseColor = new Color4(c11Vertex.Data2),
                    // Vertex blend color
                    VertexColor = new Color4(c11Vertex.Data1)
                };
            }

            return block;
        }

        #endregion Internal Methods
    }
}